package com.wangxin.study8.threadtest;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author jojo.wang
 * @title: CyclicBarrierTestA
 * @projectName study8
 * @description: TODO
 * @date 2019/5/714:37
 */

public class CyclicBarrierTestA {
    private static final int threadCount = 550;

    private static final CyclicBarrier CYCLIC_BARRIER = new CyclicBarrier(5);

    public static void main(String[] args) throws Exception{
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            Thread.sleep(1000);
            threadPool.execute(()->{
                try {
                    test(threadNum);
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
        }
        threadPool.shutdown();
    }

    public static void test (int threadNum) throws Exception{
        System.out.println("threadNum =="+threadNum+"is ready!");
        try {
            CYCLIC_BARRIER.await(60, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("threadNum:"+threadNum+"is finish!!!");
    }
}
